﻿using System.Text.Json;
using WebApplication1.Cmds;

namespace WebApplication1.Model
{
    public record AppServiceModel(string id, string name, List<AppServiceCmdModel> cmds);

    public record AppServiceCmdModel(string id,string cmd, string name, HttpCmdArgModel args);

    public record HttpCmdArgModel(string url, string method,string providerName, List<AppServiceCmdQueryParamModel> query, List<AppServiceCmdParamModel> body)
    {
        public string GetRquestUrl()
        {
            if (query == null) return url;
            var queryStr = string.Empty;
            for (var i = 0; i < query.Count; i++) { queryStr += $"{(i == 0 ? "?" : "&")}{query[i].name}={query[i].value}"; }
            return url + queryStr;
        }
        public object GetContent()
        {
            if (body == null) return string.Empty;
            var dic = new Dictionary<string, object>();
            foreach (var item in body)
            {
                object valObj = item.value;
                switch (item.valueType)
                {
                    case "Integers":
                        valObj = long.Parse(item.value);
                        break;
                    case "Decimal":
                        valObj = decimal.Parse(item.value);
                        break;
                    case "Object":
                        valObj = JsonSerializer.Deserialize<object>(item.value);
                        break;
                    default:
                        break;
                }
                dic.Add(item.name, valObj);
            }
            return dic;
        }
    }
    public record AppServiceCmdQueryParamModel(string id, string name, string value);
    public record AppServiceCmdParamModel(string id, string name, string value, string valueType)
    {

    };

    /// <summary>
    /// 参数值类型
    /// </summary>
    public enum AppServiceCmdParamValueTypeEnum
    {
        /// <summary>
        /// 整数
        /// </summary>
        Integers = 1,
        /// <summary>
        /// 字符串
        /// </summary>
        String = 2,
        /// <summary>
        /// 小数
        /// </summary>
        Decimal = 3,

        /// <summary>
        /// 对象
        /// </summary>
        Object = 4,

    }
}
